import time

import win32con

from Pose import Dynamic_Data
from pynput.keyboard import Key, Controller
from pynput import mouse
import pyautogui as pg
import win32file, win32api
import win32api

import os

pg.PAUSE = 1
keyboard = Controller()
control=mouse.Controller()
def Dynamic(lm_1shot):
    #1:维护队列
    Dynamic_Data.WINDOW.append(lm_1shot)
    if len(Dynamic_Data.WINDOW)>Dynamic_Data.WINDOW_SIZE-1:#窗口溢出
        Dynamic_Data.WINDOW.pop(0)

    #2：判断动作
    if len(Dynamic_Data.WINDOW) == Dynamic_Data.WINDOW_SIZE-1:  # 窗口刚好装满
        #2.1:计算误差
        if Right_Fist():
            win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 500, 0)
            Dynamic_Data.WINDOW=[]#清空
            return
        if Left_Fist():
            win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, -500, 0)
            Dynamic_Data.WINDOW=[]#清空
            return
        if Left_Leg():
            keyboard.press('w')
            time.sleep(1)
            keyboard.release('w')
            Dynamic_Data.WINDOW = []  # 清空
            return
        if Right_Leg():
            keyboard.press(Key.space)
            keyboard.release(Key.space)
            Dynamic_Data.WINDOW = []  # 清空
            return

       # win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 100, 0)
        if Defend():

            control.press(mouse.Button.right)
            time.sleep(1.5)
            control.release(mouse.Button.right)
            Dynamic_Data.WINDOW = []  # 清空
            return
        if cut():
            #尝试2
            control.press(mouse.Button.left)
            # 松开
            time.sleep(1)
            control.release(mouse.Button.left)
            Dynamic_Data.WINDOW = []  # 清空
            return

        #     return
        if Draw_Sword():
            keyboard.press('r')
            keyboard.release('r')
            Dynamic_Data.WINDOW = []  # 清空
            return

        if Squat():
            keyboard.press(Key.ctrl)
            keyboard.release(Key.ctrl)
            Dynamic_Data.WINDOW = []  # 清空
            return

def caculate_loss(Action):
    """
    计算误差
    :param Action: 和某个动作的误差
    :return: 误差
    """
    loss=0
    for i in range(Dynamic_Data.WINDOW_SIZE-1):
        temp_curr = Dynamic_Data.WINDOW[i]  # 当前动作 第i帧所有坐标的list
        temp_std = Action[i]  # 标准动作 第i帧所有坐标的list
        for j in range(Dynamic_Data.LM_SUM):
            loss += abs(temp_curr[j].x - temp_std[j].x) + abs(temp_curr[j].y - temp_std[j].y)
    return loss

def Right_Fist():
    """
    判断右挥拳
    :return: 判定成功或失败
    """
    RIGHT_FIST_THRESHOLD=22

    loss=caculate_loss(Dynamic_Data.Action1)

    # 判断
    #print("和标准Action1的误差=", loss)
    if loss<RIGHT_FIST_THRESHOLD:
        print("右平举臂判定成功")
        return True
    else:
        return False

def Left_Fist():
    """
    判断左挥拳
    :return: 判定成功或失败
    """
    RIGHT_FIST_THRESHOLD=22

    loss=caculate_loss(Dynamic_Data.Action2)

    # 判断
    #print("和标准Action2的误差=", loss)
    if loss<RIGHT_FIST_THRESHOLD:
        print("左平举臂判定成功")
        return True
    else:
        return False

def Left_Leg():
    """
        判断左腿抬起
        :return: 判定成功或失败
        """
    RIGHT_FIST_THRESHOLD = 0#13.5

    loss = caculate_loss(Dynamic_Data.Action3)

    # 判断
    #print("和标准Action3的误差=", loss)
    if loss < RIGHT_FIST_THRESHOLD:
        print("左腿抬起判定成功")
        return True
    else:
        return False

def Right_Leg():
    """
        判断左腿抬起
        :return: 判定成功或失败
        """
    RIGHT_FIST_THRESHOLD = 20

    loss = caculate_loss(Dynamic_Data.Action4)

    # 判断
    #print("和标准Action4的误差=", loss)
    if loss < RIGHT_FIST_THRESHOLD:
        print("右腿抬起判定成功")
        return True
    else:
        return False

def Defend():
    """
        判断左手肘抬起（防守）
        :return: 判定成功或失败
        """
    RIGHT_FIST_THRESHOLD = 22

    loss = caculate_loss(Dynamic_Data.Action5)

    # 判断
    #print("和标准Action5的误差=", loss)
    if loss < RIGHT_FIST_THRESHOLD:
        print("防守判定成功")
        return True
    else:
        return False

def cut():
    """
            判断右手横砍
            :return: 判定成功或失败
            """
    RIGHT_FIST_THRESHOLD = 17.5#如果不开游戏得25

    loss = caculate_loss(Dynamic_Data.Action6)

    # 判断
    #print("和标准Action6的误差=", loss)
    if loss < RIGHT_FIST_THRESHOLD:
        print("横砍判定成功")
        return True
    else:
        return False

def Draw_Sword():
    """
            判断双手拔剑
            :return: 判定成功或失败
            """
    RIGHT_FIST_THRESHOLD = 16.5

    loss = caculate_loss(Dynamic_Data.Action7)

    # 判断
    #print("和标准Action7的误差=", loss)
    if loss < RIGHT_FIST_THRESHOLD:
        print("拔剑判定成功")
        return True
    else:
        return False

def Squat():
    """
            判断蹲
            :return: 判定成功或失败
            """
    RIGHT_FIST_THRESHOLD = 35.5

    loss = caculate_loss(Dynamic_Data.Action8)

    # 判断
    #print("和标准Action8的误差=", loss)
    if loss < RIGHT_FIST_THRESHOLD:
        print("蹲判定成功")
        return True
    else:
        return False